<template>
	<view>
		<!--地址选择-->
		<view class="choice_addr" @click="funChoiceAddr">
			<text>{{address.ma_name?address.ma_name:$t('name')}}，{{address.ma_phone?address.ma_phone:$t('mobile')}}</text>
			<text>{{address.ma_address?address.ma_address:$t('address_alert2')}}</text>
			<icon class="iconfont icon-xiangyou"></icon>
		</view>
		<!--产品-->
		<view class="cart_con">
			<view class="con" v-for="item in list">
				<image :src="item.Product.pro_imgs"></image>
				<view class="imp">
					<text>{{item.Product.pro_title}}</text>
					<view class="tags">
						<label v-if="item.ptag_value1">{{item.ptag_value1}}</label>
					</view>
					<i>￥{{item.Product.pro_price.toFixed(2)}} <em>x{{item.cart_amount}}</em></i>
				</view>
			</view>
		</view>
		<!--底部-->
		<view class="pay_settlement">
			<view>{{$t('in_total')}} {{amount}} {{$t('piece')}}，{{$t('total')}}：<label>￥{{price}}</label></view>
			<button @click="funSubmit">{{$t('submit')}}</button>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				isLoading: true,
				list: [], //列表
				address: {}, //地址
				price: 0, //价格
				amount: 0, //数量
			}
		},
		onReady() {
			uni.setNavigationBarTitle({
				title: this.$t('pay')
			})
		},
		onLoad(options) {
			var _this = this;

			console.log(options.list);
			_this.list = JSON.parse(options.list);
			
			//计算价格
			var _price = 0;
			var _amount = 0;
			//item.Product.pro_price 这个价格已经是处理好的价格，规格赋值的
			_this.fun_map(_this.list, function(i, item) {
				_price += item.Product.pro_price * item.cart_amount;
				_amount += item.cart_amount;
			});
			_this.price = _price.toFixed(2);
			_this.amount = _amount;

			//获取默认地址
			_this.fun_request("/Api/MemberAddress/GetDefault", {}, function(res) {
				_this.address = res.data;
			}); 
		},
		methods: {
			funChoiceAddr: function() { //选择地址
				var _this = this;

			},
			funSubmit: function() { //提交订单
				var _this = this;

				if (_this.address.ma_name == undefined) {
					_this.fun_showToast("请选择您的收货地址");
					return;
				}

				//执行下单
				_this.fun_request("/Api/Orders/Pay", {
					items: JSON.stringify(_this.list),
					buyerInfo: JSON.stringify(_this.address),
					remark: ""
				}, function(res) {
					console.log(res);
					_this.fun_showToast(res.msg);

					//更新购物车
					uni.$emit('load_cart');
				});
			},
			funHref: function(url) { //跳转
				this.fun_navigateTo(url);
			},
		}
	}
</script>

<style lang="scss">
	page {
		background-color: #F7F7F7;
		padding-bottom: 130rpx;
	}

	.choice_addr {
		background-color: #fff;
		border-radius: 15rpx;
		margin: 20rpx 3% 0;
		width: 94%;
		padding: 25rpx 3%;
		position: relative;

		text {
			display: block;
			width: 100%;
			font-size: 28rpx;
			line-height: 45rpx;
			color: #F85F69;
		}

		icon {
			position: absolute;
			right: 2%;
			top: 40rpx;
			font-size: 40rpx;
			color: #666;
		}
	}

	.pay_settlement {
		position: fixed;
		left: 0;
		bottom: 0;
		width: 100%;
		background-color: #fff;
		height: 100rpx;
		line-height: 70rpx;
		padding: 15rpx 3%;
		font-size: 30rpx;

		view {
			float: left;

			label {
				color: #F85F69;
			}
		}

		button {
			float: right;
			height: 70rpx;
			line-height: 70rpx;
			background-color: #F85F69;
			color: #fff;
			border-radius: 50rpx;
			font-size: 30rpx;
			padding: 0 40rpx;
		}
	}
</style>
